package com.eascs.ticket.ticket.controller;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.ticket.form.StatWorkForm;
import com.eascs.ticket.user.entity.User;
import com.eascs.ticket.user.service.UserService;
import com.eascs.web.context.RequestContext;
import com.eascs.web.exception.RedirectException;
import com.eascs.web.sso.UserHolder;

@Controller
public class StatWorkViewController extends BaseMasterViewController {

	@Inject
	private UserService userService;

	@RequestMapping("/stat/view/work")
	public String statWork(StatWorkForm statWorkForm) {
		String codes = statWorkForm.getOperators();
		if (StringUtil.isBlank(codes)) {
			codes = UserHolder.getUser().getCode();
		}
		List<User> users = userService.getUsersByCodes(StringUtil.split(codes));
		ViewContext.put("users", users);
		ViewContext.put("statWorkForm", statWorkForm);

		String userStatUrl = RequestContext.getRequestUrlWithQueryString();
		userStatUrl = UrlUtil.appendUrl(UrlUtil.removeParam(userStatUrl, "operators"), "_ajax", System.currentTimeMillis() + "");
		userStatUrl = "/stat/ajax/work" + userStatUrl.substring(userStatUrl.indexOf('?'));
		ViewContext.put("userStatUrl", userStatUrl);

		return "/stat/view/work";
	}

	@RequestMapping("/stat/view/work/this-week")
	public void statWorkThisWeek() {
		String url = "/stat/view/work?ticketTypeCodeNotIns=overtime&ticketTypeCodeNotIns=leave&statusChangeIns=NEWl10pkoth64dgq&statusChangeIns=NEWSYSTEM_DEV_REVIEW&statusChangeIns=NEWREADY_IMPLEMENT&statusChangeIns=READY_IMPLEMENTSYSTEM_DEV_REVIEW&statusChangeIns=MERGEINGMERGE_FINISH";
		Date startDate = DateUtils.getMondayOfThisWeek();
		Date endDate = DateUtils.addDate(startDate, 6);
		url = UrlUtil.appendUrl(url, "statDateStart", DateUtils.formateStandDate(startDate));
		url = UrlUtil.appendUrl(url, "statDateEnd", DateUtils.formateStandDate(endDate));
		url = UrlUtil.appendUrl(url, "operators", UserHolder.getUser().getCode());
		throw new RedirectException(url);
	}

	@RequestMapping("/stat/view/work/last-week")
	public void statWorkLastWeek() {
		String url = "/stat/view/work?ticketTypeCodeNotIns=overtime&ticketTypeCodeNotIns=leave&statusChangeIns=NEWl10pkoth64dgq&statusChangeIns=NEWSYSTEM_DEV_REVIEW&statusChangeIns=NEWREADY_IMPLEMENT&statusChangeIns=READY_IMPLEMENTSYSTEM_DEV_REVIEW&statusChangeIns=MERGEINGMERGE_FINISH";
		Date startDate = DateUtils.getMondayOfThisWeek();
		startDate = DateUtils.addDate(startDate, -7);
		Date endDate = DateUtils.addDate(startDate, 6);
		url = UrlUtil.appendUrl(url, "statDateStart", DateUtils.formateStandDate(startDate));
		url = UrlUtil.appendUrl(url, "statDateEnd", DateUtils.formateStandDate(endDate));
		url = UrlUtil.appendUrl(url, "operators", UserHolder.getUser().getCode());
		throw new RedirectException(url);
	}

}
